package com.cloud.pole.weixin.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.weixin.biz.WxUserBiz;
import com.cloud.pole.weixin.model.entity.WxApp;
import com.cloud.pole.weixin.model.entity.WxUser;
import com.cloud.pole.weixin.service.WxUserService;
import com.cloud.pole.weixin.wechat.domain.UserInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年4月24日
*cloud-weixin
*author:frank-chen
*/
@Service
@Slf4j
public class WxUserServiceImpl implements WxUserService {
	@Autowired 
	private WxUserBiz wxUserBiz;
	
	@Override
	public WxUser getWxUser(String openId) throws Exception {
		// TODO Auto-generated method stub
		WxUser wxUser = wxUserBiz.selectByOpenId(openId);
		if(wxUser == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "openId错误");
		}	
		return wxUser;
	}
	
	@Override
	public void modifyWxUser(UserInfo userInfo, WxApp wxApp) throws Exception {
		WxUser wxUser = wxUserBiz.selectByOpenId(userInfo.getOpenid());
		if(wxUser == null) {
			wxUser = new WxUser();
			wxUser.setAppId(wxApp.getAppId());
			wxUser.setAvatarUrl(userInfo.getHeadimgurl());
			wxUser.setCity(userInfo.getCity());
			wxUser.setCountry(userInfo.getCountry());
			wxUser.setCreateTime(new Date());
			wxUser.setGender(userInfo.getSex());
			wxUser.setNickName(userInfo.getNickname());
			wxUser.setOpenId(userInfo.getOpenid());
			wxUser.setProvince(userInfo.getProvince());
			wxUser.setUnionId(userInfo.getUnionid());
			wxUser.setWxAppId(wxApp.getWxAppId());
			Integer status = userInfo.getSubscribe();
			if(status != null) {
				String subscribeTime = userInfo.getSubscribe_time();
				Date time = null;
				if(!StringUtils.isEmpty(subscribeTime)) {
					time = new Date(Long.parseLong(subscribeTime));
				}
				if(status == UserInfo.SUBSCRIBE_NO) {
					wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_NO);
					if(time != null) {
						wxUser.setUnsubscribeTime(time);
					}
				}
				else if(status == UserInfo.SUBSCRIBE_YES) {
					wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_YES);
					if(time != null) {
						wxUser.setSubscribeTime(time);
					}
				}
			}
			else {
				wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_DEFAULT);
			}
			wxUserBiz.insertSelective(wxUser);
		}
		else {
			wxUser.setAppId(wxApp.getAppId());
			wxUser.setAvatarUrl(userInfo.getHeadimgurl());
			wxUser.setCity(userInfo.getCity());
			wxUser.setCountry(userInfo.getCountry());
			wxUser.setCreateTime(new Date());
			wxUser.setGender(userInfo.getSex());
			wxUser.setNickName(userInfo.getNickname());
			wxUser.setProvince(userInfo.getProvince());
			wxUser.setUnionId(userInfo.getUnionid());
			wxUser.setWxAppId(wxApp.getWxAppId());
			Integer status = userInfo.getSubscribe();
			if(status != null) {
				String subscribeTime = userInfo.getSubscribe_time();
				Date time = null;
				if(!StringUtils.isEmpty(subscribeTime)) {
					time = new Date(Long.parseLong(subscribeTime));
				}
				if(status == UserInfo.SUBSCRIBE_NO) {
					wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_NO);
					if(time != null) {
						wxUser.setUnsubscribeTime(time);
					}
				}
				else if(status == UserInfo.SUBSCRIBE_YES) {
					wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_YES);
					if(time != null) {
						wxUser.setSubscribeTime(time);
					}
				}
			}
			wxUserBiz.updateByPrimaryKey(wxUser);
		}
	}

	@Override
	public void unSubscribeWxUser(String openId) throws Exception {
		// TODO Auto-generated method stub
		WxUser wxUser = wxUserBiz.selectByOpenId(openId);
		if(wxUser != null) {
			wxUser.setSubscribeStatus(WxUser.SUBSCRIBESTATUS_NO);
			wxUser.setUnsubscribeTime(new Date());
			wxUserBiz.updateByPrimaryKey(wxUser);
		}
	}
}
